package com.example.lab02.controller;

import com.example.lab02.entity.User;
import com.example.lab02.service.UserService;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("user")
public class UserController extends BaseController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("queryAll")
    private String queryAll(Model model){
        model.addAttribute("users",userService.queryAll());
        return "/user";
    }

    @PostMapping("create")
    private String create(User user){
        userService.create(user);
        return "redirect:/user/queryAll";
    }
    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable int id , Model model){
        session.setAttribute("id",id);
        model.addAttribute("user",userService.queryById(id));
        return "/edit";
    }
    @PostMapping("modify")
    private String modify(User user){
        user.setId((Integer) session.getAttribute("id"));
        userService.modify(user);
        return "redirect:/user/queryAll";
    }
    @GetMapping("queryForPage/{page}")
    @ResponseBody
    private Page<User> queryForPage(@PathVariable int page){
        return userService.queryForPage(page);
    }


}
